package com.lqjava.tuxiaowang.tuxiaowang.customerapp.app.api.open;

import com.lqjava.temp.app.entity.request.MiniProgramLoginRequest;
import com.lqjava.temp.app.entity.response.MiniProgramLoginResponse;
import com.lqjava.temp.app.enums.MiniProgramTypeEnum;
import com.lqjava.temp.app.sdk.ali.ALIPayMPSDK;
import com.lqjava.temp.app.sdk.wechat.WechatMPSDK;
import com.lqjava.temp.core.exceptions.ApiParamsException;
import com.lqjava.temp.core.utils.ParamsHelper;
import com.lqjava.temp.core.web.result.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/open/mp")
public class MiniProgramIndexApi {

	
	@Autowired
	WechatMPSDK wechatMPSDK;
	
	@Autowired
	ALIPayMPSDK aliPayMPSDK;
	
	/**
	 * 聚合登录接口
	 * @param miniProgramType 可选值 miniProgramTypeEnum
	 * @param request 请求参数,code,appid
	 * @return 登录的sessionKey
	 */
	@RequestMapping(value = "/{miniProgramType}/login",method = RequestMethod.POST)
	public ResultBean<MiniProgramLoginResponse> wechatIndex(
			@PathVariable("miniProgramType") String miniProgramType,@RequestBody MiniProgramLoginRequest request){
		//小程序类型验证
		MiniProgramTypeEnum miniProgramTypeEnum = MiniProgramTypeEnum.get(miniProgramType);
		if(miniProgramTypeEnum == null){
			ApiParamsException.throwBusiness("无效的小程序类型");
		}
		//必填验证
		ParamsHelper.requiredValidator(request, "appid","code");
		
		ResultBean<MiniProgramLoginResponse> res = new ResultBean<>();
		
		//登录
		MiniProgramLoginResponse response = miniProgramTypeEnum == MiniProgramTypeEnum.WECHAT ? wechatMPSDK.login(request) : aliPayMPSDK.login(request);
		res.initTrue(response);
		
		return res;
	}
	
	
	
}
